<?php $this->layout('layout') ?>
<?php $this->start('main') ?>

<?php
if (is_null($task)) {
  $task             = (object) array();
  $task->id         = 0;
  $task->title      = '没有标题的标题';
  $task->caty       = 0;
  $task->priority   = 99;
  $task->level      = 10;
  $task->department = $authed->department;
  $task->leader     = $authed->id;
  $task->pro        = 0;
  $task->tag        = 0;
  $task->tester     = $authed->id;
  $task->qa         = $authed->id;
  $task->deadline   = time() + 86400;
  $task->status     = 12;
  $task->content    = '';

  if ($related) {
    $task->title    = $related->title;
    $task->level    = $related->level;
    $task->pro      = $related->pro;
    $task->tag      = $related->tag;
    $task->priority = $related->priority;
  }
}
$status = I\App::singleton()->getconfig('worktime', 'status');
?>
<h1><?= ($related ? sprintf('上级任务：#%s %s', $related->id, $related->title) : '提交新任务') ?></h1>
<hr />

<form method="POST" action="/task/store" onsubmit="return oncommit( );">

  <input id="task-id" type="hidden" name="id" value="<?= $task->id ?>" />
  <input type="hidden" name="lockn" value="<?= $lockn ? $lockn : 0 ?>">
  <input type="hidden" id="repeated" value="1">
  <input type="hidden" id="taskContent" name="row[content]">
  <input type="hidden" name="row[related]" value="<?= ($related ? $related->id : 0) ?>">

  <div class="form-row mb-3">
    <div class="form-inline">

      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text">标题</span>
        </div>
        <input id="task-title" name="row[title]" type="text" style="width:380px" class="form-control" value="<?= str_replace(array('\'', '"'), array('&apos;', '&quot;'), $task->title) ?>">

        <select style="width: 150px;" class="form-control" onchange="onChangePro(this.value, '#update-tag', '版本');">
          <option value="0">选择项目</option>
          <?php $this->insert('selection-users', ['data' => $pros, 'slt' => $task->pro]) ?>
        </select>
        <select style="width: 150px;" name="row[tag]" class="form-control" id="update-tag">
          <option value="0">未选项目</option>
          <?php $this->insert('selection-users', ['data' => $tags, 'slt' => $task->tag]) ?>
        </select>

        <select style="width: 100px;" name="row[priority]" class="form-control">
          <?php
          $prioritys = I\App::singleton()->getconfig('worktime', 'priority');
          $this->insert('selection', ['data' => $prioritys, 'slt' => $task->priority]) ?>
        </select>
        <input id="update-level" name="row[level]" type="number" style="width:100px" class="form-control" value="<?= $task->level ?>">

        <select style="width: 100px;" id="caty" name="row[caty]" class="form-control">
          <?php $this->insert('selection-users', ['data' => $catys, 'slt' => $task->caty]) ?>
        </select>

      </div>


      <div class="input-group ml-2">
        <div class="input-group-prepend">
          <span class="input-group-text">线上</span>
        </div>
        <select name="row[isonline]" class="form-control">
          <?php $this->insert('selection', ['data' => [0 => '否', 1 => '是'], 'slt' => 0]) ?>
        </select>
      </div>

      <div class="input-group ml-2">
        <div class="input-group-prepend">
          <span class="input-group-text">状态</span>
        </div>
        <select id="update-status" name="row[status]" class="form-control">
          <option value="0">未选择</option>
          <?php $this->insert('selection', ['data' => $status, 'slt' => $task->status]) ?>
        </select>
      </div>

    </div>
  </div>

  <div class="form-row mb-3">
    <div class="form-inline">

      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text">部门</span>
        </div>
        <select class="form-control" onchange="onChangeDepartment( this.value, '#update-leader' )">
          <?php $this->insert('selection-users', ['data' => $departments, 'slt' => $task->department]) ?>
        </select>
      </div>

      <div class="input-group ml-2">
        <div class="input-group-prepend">
          <span class="input-group-text">负责人</span>
        </div>
        <select name="row[leader]" class="form-control" id="update-leader">
          <?php foreach ($users as $user) : ?>
            <?php if ($user->department == $task->department) : ?>
              <option value="<?= $user->id ?>" <?= $user->id == $task->leader ? 'selected' : '' ?>><?= $user->name ?></option>
            <?php endif ?>
          <?php endforeach ?>
        </select>
      </div>


      <div class="input-group ml-2">
        <div class="input-group-prepend">
          <span class="input-group-text">验收部门</span>
        </div>
        <select class="form-control" onchange="onChangeDepartment( this.value, '#update-tester' )">
          <?php
          $tester = $users[$task->tester];
          $this->insert('selection-users', ['data' => $departments, 'slt' => $tester->department]);
          ?>
        </select>
      </div>

      <div class="input-group ml-2">
        <div class="input-group-prepend">
          <span class="input-group-text">验收</span>
        </div>
        <select name="row[tester]" class="form-control" id="update-tester">
          <?php foreach ($users as $user) : ?>
            <?php if ($user->department == $tester->department) : ?>
              <option value="<?= $user->id ?>" <?= $user->id == $task->tester ? 'selected' : '' ?>><?= $user->name ?></option>
            <?php endif ?>
          <?php endforeach ?>
        </select>
      </div>

      <div class="input-group ml-2">
        <div class="input-group-prepend">
          <span class="input-group-text">品控</span>
        </div>
        <select name="row[qa]" class="form-control" id="update-tester">
          <?php foreach ($users as $user) : ?>
            <?php if ($user->department == 305) : ?>
              <option value="<?= $user->id ?>" <?= $user->id == $task->qa ? 'selected' : '' ?>><?= $user->name ?></option>
            <?php endif ?>
          <?php endforeach ?>
        </select>
      </div>
    </div>
  </div>

  <textarea id="summernote" height="500"><?= $task->content ?></textarea>

  <div class="mb-3"></div>
  <button type="submit" class="btn btn-primary btn-lg btn-block"> 提 交 </button>
</form>

<?php $this->end() ?>


<?php $this->start('script') ?>
<script type="text/javascript">
var users = <?= json_encode($users) ?>;
var tags = <?= json_encode($tags) ?>;

$(document).ready(function() {
  initEditor("summernote");
});

function oncommit() {
  if ($("#repeated").val() < 1) {
    return false;
  }

  if ($("#update-level").val() <= 0) {
    alert('没有设定优先等级');
    return false;
  }

  if ($("#update-status").val() <= 0) {
    alert('没有设定状态');
    return false;
  }

  if ($("#update-leader").val() <= 0) {
    alert('没有选择部门或者负责人');
    return false;
  }

  var tag_id = $("#update-tag").val()
  if (tag_id <= 0) {
    alert('没有选择项目或者版本');
    return false;
  }

  var caty_id = $("#caty").val()
  if (caty_id != 102 && tags[tag_id].lock) {
    alert('本版本已经被锁住。@杨凯允');
    return false;
  }

  var c = $('#summernote').summernote('code');
  if (c.indexOf("data:image/png;base64") > 0) {
    alert('不正确的图片格式，不要从word、有道等软件中直接粘贴过来，建议使用ctl+shift+v');
    return false;
  }
  $('#taskContent').val(c);

  $("#repeated").val("0")

  return true;
}

</script>
<?php $this->end() ?>
